//
//  BuyDateViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-23.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "BuyDateViewController.h"
#import "ChassisNumViewController.h"
@interface BuyDateViewController () <UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIButton *_btnLast;
    UIButton *_btnNext;
    UIPickerView *_pickerView;
    NSMutableArray *_yearArray;
    NSArray *_monthArray;
}
@end

@implementation BuyDateViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _yearArray = [NSMutableArray array];
        for (int i=1949; i<2100; i++) {
            [_yearArray addObject:[NSString stringWithFormat:@"%i年", i]];
        }
        _monthArray = @[@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"注册-车辆验证"];
    [self configView];
}

- (void)configView
{
    //购买日期label
    UILabel *nameLabel = [self createLabelFrame:CGRectMake(30*W, 110*H, SCREEN_WIDTH-60*W, 30*H) title:@"购买日期" font:[UIFont systemFontOfSize:26]];
    [self.view addSubview:nameLabel];
    
    //PickerView
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.center = CGPointMake(self.view.center.x, self.view.center.y+10*H);
    [_pickerView setBackgroundColor:[UIColor clearColor]];
    _pickerView.delegate = self;
    [_pickerView selectRow:65 inComponent:1 animated:YES];
    [_pickerView selectRow:9 inComponent:2 animated:YES];
    [self.view addSubview:_pickerView];
    
    //上一步按钮
    _btnLast = [self addLastBtnTarget:self action:@selector(btnLastClicked:)];
    [self enableButton:_btnLast];
    
    //下一步按钮
    _btnNext = [self addNextBtnTarget:self action:@selector(btnNextClicked:)];
    [self enableButton:_btnNext];
}

#pragma mark - 点击事件
//上一步，返回上一个界面
- (void)btnLastClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//下一步，进入新页面
- (void)btnNextClicked:(id)sender
{
    NSInteger numY = [_pickerView selectedRowInComponent:1];
    NSInteger numM = [_pickerView selectedRowInComponent:2];
    NSString *oldYear = _yearArray[numY];
    NSString *oldMonth = _monthArray[numM];
    NSRange rangeY = [oldYear rangeOfString:@"年"];
    NSRange rangeM = [oldMonth rangeOfString:@"月"];
    NSString *year = [oldYear substringToIndex:rangeY.location];
    NSString *month = [oldMonth substringToIndex:rangeM.location];
    NSString *buyTime = [NSString stringWithFormat:@"%@-%@",year, month];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:buyTime forKey:@"buyTime"];
    
    ChassisNumViewController *cnVC = [ChassisNumViewController new];
    [self.navigationController pushViewController:cnVC animated:YES];
}

#pragma mark- UIPickerView代理函数
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return 0;
    }
    else if (component == 1) {
        return _yearArray.count;
    }
    else if (component == 2) {
        return _monthArray.count;
    }
    else if (component == 3) {
        return 0;
    }
    else {
        return 0;
    }
}

//一共多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 4;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel *)view;
    if (component == 1) {
        if (!pickerLabel) {
            pickerLabel = [UILabel new];
            pickerLabel.text = _yearArray[row];
            pickerLabel.textAlignment = NSTextAlignmentCenter;
            pickerLabel.textColor = [UIColor whiteColor];
        }
    }
    else if (component == 2) {
        if (!pickerLabel) {
            pickerLabel = [UILabel new];
            pickerLabel.text = _monthArray[row];
            pickerLabel.textAlignment = NSTextAlignmentCenter;
            pickerLabel.textColor = [UIColor whiteColor];
        }
    }
    return pickerLabel;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
